package com.dc.project_kotlin.ui.activity

import android.content.Intent
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.dc.base.vm.BaseViewModel
import com.dc.project_kotlin.MainActivity
import com.dc.project_kotlin.adapter.WelcomeBannerAdapter
import com.dc.project_kotlin.base.BaseActivity
import com.dc.project_kotlin.databinding.ActivityWelcomeBinding
import com.dc.project_kotlin.ext.gone
import com.dc.project_kotlin.ext.visible
import com.dc.project_kotlin.utils.CacheUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class WelcomeActivity : BaseActivity<BaseViewModel, ActivityWelcomeBinding>() {

    private val reList = arrayListOf("1", "2", "3")

    override fun initView() {
       mViewBinding.welcomeJoin.setOnClickListener {
           CacheUtil.setFirst(false)
           startActivity(Intent(this, MainActivity::class.java))
           finish()

           overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
       }
        if (CacheUtil.isFirst()) {
            mViewBinding.welcomeImage.gone()
            mViewBinding.bannerView.apply {
                adapter = WelcomeBannerAdapter(mActivity, reList)
                registerOnPageChangeCallback(object : OnPageChangeCallback() {
                    override fun onPageSelected(position: Int) {
                        if (position == reList.size - 1) {
                            mViewBinding.welcomeJoin.visible()
                        } else {
                            mViewBinding.welcomeJoin.gone()
                        }
                    }
                })
            }
        } else {
            mViewBinding.welcomeImage.visible()
            lifecycleScope.launch {
                delay(300)
                withContext(Dispatchers.Main) {
                    startActivity(Intent(mActivity, MainActivity::class.java))
                    mActivity.finish()
                    mActivity.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
                }
            }
        }
    }

    override fun initData() {

    }

}